﻿using GN.Pay.Core.Entities;
using GN.Pay.Core.Values;
using GN.Pay.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.GatewayWeb.JumpNotify
{
    /// <summary>
    /// 申请会话
    /// </summary>
    public class HubApplySession
    {
        private readonly Dictionary<string, HubSession> clients;

        /// <summary>
        /// 实例化 HubSession 类新实例
        /// </summary>
        /// <param name="applyId">申请Id</param>
        public HubApplySession(string applyId)
        {
            ApplyId = applyId;
            clients = new Dictionary<string, HubSession>();
        }

        /// <summary>
        /// 获取申请Id
        /// </summary>
        public string ApplyId { get; private set; }

        /// <summary>
        /// 注册客户端
        /// </summary>
        /// <param name="session">客户端</param>
        public void RegisterClient(HubSession session)
        {
            if (session != null)
            {
                lock (clients)
                {
                    clients[session.ConnectionId] = session;
                }
            }
        }

        /// <summary>
        /// 移除客户端
        /// </summary>
        /// <param name="connectionId">>连接Id</param>
        public bool RemoveClient(string connectionId)
        {
            if (connectionId != null)
            {
                lock (clients)
                {
                    return clients.Remove(connectionId);
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取客户端集合
        /// </summary>
        public ICollection<HubSession> Clients
        {
            get
            {
                lock (clients)
                {
                    return clients.Values;
                }
            }
        }

        /// <summary>
        /// 清除
        /// </summary>
        public void Clear()
        {
            lock (clients)
            {
                clients.Clear();
            }
        }

        /// <summary>
        /// 获取客户端数量
        /// </summary>
        public int Count
        {
            get
            {
                lock (clients)
                {
                    return clients.Count;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="send"></param>
        /// <returns></returns>
        private async Task<int> SendNotify(Func<HubSession, Task<bool>> send)
        {
            int count = 0;
            var items = Clients.ToArray();
            foreach (var item in items)
            {
                if (await send(item))
                {
                    count++;
                }
            }
            return count;
        }

        /// <summary>
        /// 付款成功通知
        /// </summary>
        /// <param name="payNotifyData">数据</param>
        public async Task<int> PaySuccess(TradeNotify tradeNotify)
        {
            return await SendNotify(s => s.PaySuccess(tradeNotify));
        }

        /// <summary>
        /// 通知成功通知
        /// </summary>
        /// <param name="payNotifyData">数据</param>
        public async Task<int> NotifySuccess(TradeNotify tradeNotify)
        {
            return await SendNotify(s => s.NotifySuccess(tradeNotify));
        }
    }
}